1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class Brick : MonoBehaviour
6 {
7
8     
public AudioClip Laser;
9     
public Sprite[] hitSprites;
10     
public static int breakableCount = 0;
11     
public GameObject smoke;
12
13     
//private int maxHits;
14     
private int timesHit;
15     
private LevelManager levelManager;
16     
private bool isBreakable;
17
18     
// Use this for initialization
19     
void Start()
20     {
21         isBreakable = (
this.tag == "Breakable");
22         
// Tracking of breakable Bricks
23         
if (isBreakable)
24         {
25             breakableCount++;
26         }
27
28         timesHit =
0;
29         levelManager = GameObject.FindObjectOfType<LevelManager>();
30     }
31
32     
// Update is called once per frame
33     
void Update()
34     {
35         
36     }
37
38     
void OnCollisionEnter2D(Collision2D col)
39     {
40         AudioSource.PlayClipAtPoint(Laser, transform.position);
41         
if (isBreakable)
42         {
43             HandleHits();
44         }
45     }
46
47     
void HandleHits ()
48     {
49         timesHit++;
50         
int maxHits = hitSprites.Length + 1;
51         
if (timesHit >= maxHits)
52         {
53             breakableCount--;
54             levelManager.BrickDestroyed();
55             PuffSmoke();
56             Destroy(gameObject);
57         }
58         
else
59         {
60             LoadSprites();
61         }
62     }
63
64     
void PuffSmoke()
65     {
66         GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity)
as GameObject;
67         ParticleSystem.MainModule main = smokePuff.GetComponent<ParticleSystem>().main;
68         main.startColor = gameObject.GetComponent<SpriteRenderer>().color;
69     }
70
71     
void LoadSprites()
72     {
73         
int spriteIndex = timesHit - 1;
74         
if (hitSprites[spriteIndex] !=null)
75         {
76             
this.GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
77         }
78         
else
79         {
80             Debug.LogError (
"Brick Sprite Missing");
81         }
82     }
83
84     
// TODO Remove this method once we can actually win!
85     
void SimulateWin() {
86         levelManager.LoadNextLevel();
87     }
88 }


Gõ tìm kiếm nhanh...